<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Source: easynode/Logger.js</title>
    
    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">
    
    <h1 class="page-title">Source: easynode/Logger.js</h1>
    
    


    
    <section>
        <article>
            <pre class="prettyprint source"><code>var log4js = require('log4js');
var fs = require('fs');

(function(){
        //singleton mode
        var instance = null;
        /**
         * @since 1.0
         * @author zlbbq
         * @class
         * @desc Logger of EasyNode. This is a singleton class. DO NOT create any instance of Logger, use getInstance() instead.
         * @example
         *
         * using('easynode.Logger');
         * var logger = easynode.Logger.getLogger('loggerName');
         * logger.debug('hello');
         *
         * var logger = easynode.Logger.forFile(__filename);
         * logger.debug('hello');
         * */
        easynode.Logger =  function() {
                if(instance) {
                        throw new Error('easynode.Logger is a singleton class,  use getInstance() or getLogger() instead of instantiation');
                }
                //initialize console and root logger
                var appenders = [
                        {
                                type: 'console'
                        }
                ];
                var logFolder = config('easynode.logger.folder');
                if(!fs.existsSync(real(logFolder))) {
                        fs.mkdirSync(real(logFolder));
                }

                var s = config('easynode.logger.appenders') || '';
                s = s.split(',');
                s.forEach(function(v){
                        v = trim(v);
                        var appender = {
                                type: 'dateFile',
                                filename: real(logFolder + '/' + config('easynode.logger.appender.'+v+'.file')),
                                pattern: config('easynode.logger.appender.'+v+'.pattern'),
                                maxLogSize: parseInt(config('easynode.logger.appender.'+v+'.maxSize')),
                                alwaysIncludePattern: false,
                                backups: parseInt(config('easynode.logger.appender.'+v+'.backup')),
                                category: config('easynode.logger.appender.'+v+'.namespace') || v
                        };
                        appenders.push(appender);
                });
                log4js.configure({
                        appenders : appenders,
                        replaceConsole: true
                });
        };

        /**
         * @since 1.0
         * @author zlbbq
         * @static
         * @function
         * @return {easynode.Logger} Logger instance
         * @desc Get the singleton instance of Logger
         * @example
         *
         * using('easynode.Logger');
         * var logger = easynode.Logger.getLogger.getInstance();
         * logger.getLogger().debug('hello');
         * logger.getLogger().info('hello');
         * logger.getLogger().warn('hello');
         * logger.getLogger().error('hello');
         * logger.getLogger().fatal('hello');
         * */
        easynode.Logger.getInstance = function() {
                if(!instance) {
                        instance = new easynode.Logger();
                }
                return instance;
        };

        /**
         * @since 1.0
         * @author zlbbq
         * @static
         * @function
         * @param {String} name Logger name, default value is 'root'
         * @return {Object} Logger instance. A logger instance has debug, info, warn, error, fatal methods mapping 5 log levels DEBUG, INFO, WARN, ERROR, FATAL
         * @desc Get the singleton instance of Logger
         * @example
         *
         * using('easynode.Logger');
         * var logger = easynode.Logger.getLogger.getInstance();
         * logger.getLogger().debug('hello');
         * logger.getLogger('loggerName').debug('hello');
         * */
        easynode.Logger.getLogger = function(name) {
                var inst = easynode.Logger.getInstance();
                return inst.getLogger(name);
        };

        /**
         * @since 1.0
         * @author zlbbq
         * @static
         * @function
         * @param {String} fileName File name, pass __dirname or __filename
         * @return {Object} Logger instance. A logger instance has debug, info, warn, error, fatal methods mapping 5 log levels DEBUG, INFO, WARN, ERROR, FATAL
         * @desc Get Logger for a file
         * @example
         *
         * using('easynode.Logger');
         * var logger = easynode.Logger.getLogger.getInstance();
         * logger.getLogger().debug('hello');
         * logger.getLogger('loggerName').debug('hello');
         * */
        easynode.Logger.forFile = function(fileName) {
                var inst = easynode.Logger.getInstance();
                return inst.forFile(fileName);
        };

        /**
         * @since 1.0
         * @author zlbbq
         * @param {String} name Logger name, default is 'root'
         * @return {Object} Logger instance. A logger instance has debug, info, warn, error, fatal methods mapping 5 log levels DEBUG, INFO, WARN, ERROR, FATAL
         * @desc Get a log4js logger then apply log level configured. Do not new easynode.Logger() then call this function, use static function
         * easynode.Logger.getLogger instead.
         * @private
         * */
        easynode.Logger.prototype.getLogger = function(name) {
                name = name || 'root';
                var logger = log4js.getLogger(name);
                var l = config('easynode.logger.appender.' + name + '.' + 'level') || config('easynode.logger.level');
                l && logger.setLevel(l);
                return logger;
        };

        /**
         * @since 1.0
         * @author zlbbq
         * @param {String} file File name, pass __dirname or __filename
         * @return {Object} Logger instance. A logger instance has debug, info, warn, error, fatal methods mapping 5 log levels DEBUG, INFO, WARN, ERROR, FATAL
         * @desc Get a log4js logger for a file or directory then apply log level configured. Do not new easynode.forFile() then call this function, use static function
         * easynode.Logger.getLogger instead.
         * It is strongly recommended that use logger
         * @private
         * */
        easynode.Logger.forFile = function(file) {
                var ns = namespace(file);
                return this.getLogger(ns);
        };

        //config log4js automatically when Logger is using
        easynode.Logger.getInstance();

        module.exports = easynode.Logger;
})();</code></pre>
        </article>
    </section>




</div>

<nav>
    <h2><a href="index.html">Index</a></h2><h3>Classes</h3><ul><li><a href="easynode.Logger.html">Logger</a></li></ul><h3>Namespaces</h3><ul><li><a href="easynode.html">easynode</a></li><li><a href="easynode.framework.html">framework</a></li><li><a href="easynode.framework.server.html">server</a></li><li><a href="easynode.test.html">test</a></li></ul><h3>Global</h3><ul><li><a href="global.html#addSourceFolder">addSourceFolder</a></li><li><a href="global.html#arg">arg</a></li><li><a href="global.html#config">config</a></li><li><a href="global.html#extend">extend</a></li><li><a href="global.html#home">home</a></li><li><a href="global.html#namespace">namespace</a></li><li><a href="global.html#real">real</a></li><li><a href="global.html#resolveNamespaces">resolveNamespaces</a></li><li><a href="global.html#trim">trim</a></li><li><a href="global.html#using">using</a></li></ul>
</nav>

<br clear="both">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a> on Sat May 09 2015 09:08:52 GMT+0800 (CST)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>
